export const calcDate = (date1, date2) => {
  let date3 = date2 - date1;

  let days = Math.floor(date3 / (24 * 3600 * 1000))

  let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
  let hours = Math.floor(leave1 / (3600 * 1000))

  let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
  let minutes = Math.floor(leave2 / (60 * 1000))

  let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
  let seconds = Math.round(date3 / 1000)
  return {
    leave1,
    leave2,
    leave3,
    days: days,
    hours: hours,
    minutes: minutes,
    seconds: seconds,
  }
}

/**
 * 日期格式化
 */
export function dateFormat(date, format) {
  format = format || 'yyyy-MM-dd hh:mm:ss';
  if (date !== 'Invalid Date') {
    let o = {
      "M+": date.getMonth() + 1, //month
      "d+": date.getDate(), //day
      "h+": date.getHours(), //hour
      "m+": date.getMinutes(), //minute
      "s+": date.getSeconds(), //second
      "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
      "S": date.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
      if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
          RegExp.$1.length === 1 ? o[k] :
            ("00" + o[k]).substr(("" + o[k]).length));
    return format;
  }
  return '';

}
//过去一个月的时间
function getPassFormatDate() {
  var nowDate = new Date();
  var date = new Date(nowDate);
  date.setDate(date.getDate() - 30);
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}
//过去3个月的时间
export function getPassThreeDate() {
  var nowDate = new Date();
  var date = new Date(nowDate);
  date.setDate(date.getDate() - 90);
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}

//过去一年的时间
function getPassYearFormatDate() {
  var nowDate = new Date();
  var date = new Date(nowDate);
  date.setDate(date.getDate() - 365);
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}

export function GetMonday(dd) {
  var week = dd.getDay(); //获取时间的星期数
  var minus = week ? week - 1 : 6;
  dd.setDate(dd.getDate() - minus); //获取minus天前的日期
  var y = dd.getFullYear();
  var mon = dd.getMonth() + 1
  var m = mon > 9 ? mon : '0' + mon; //获取月份
  var da = dd.getDate();
  var d = da > 9 ? da : '0' + da;
  return y + "-" + m + "-" + d;
}

//根据当前周某天日期  获取周三日期
export function GetWednesday(dd) {
  var week = dd.getDay(); //获取时间的星期数
  var minus = week ? week - 1 : 6;
  dd.setDate(dd.getDate() - minus + 2); //获取minus天前2天后的日期
  var y = dd.getFullYear();
  var mon = dd.getMonth() + 1
  var m = mon > 9 ? mon : '0' + mon; //获取月份
  var da = dd.getDate();
  var d = da > 9 ? da : '0' + da;
  return y + "-" + m + "-" + d;
}

export function GetSunday(dd) {
  var week = dd.getDay(); //获取时间的星期数0~6
  var minus = week ? 7 - week : 0;
  dd.setDate(dd.getDate() + minus); //获取minus天后的日期
  var y = dd.getFullYear();
  var mon = dd.getMonth() + 1
  var m = mon > 9 ? mon : '0' + mon; //获取月份
  var da = dd.getDate();
  var d = da > 9 ? da : '0' + da;
  return y + "-" + m + "-" + d;
}

//获取一年中第几周
export const getYearWeek = (date) => {
  var totalDays = 0;
  var myDate = new Date(date);
  var year = myDate.getFullYear();
  var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

  if (myDate.getMonth() == 0) {
    totalDays = totalDays + myDate.getDate();
  } else {
    var curMonth = myDate.getMonth();
    for (var count = 1; count <= curMonth; count++) {
      totalDays = totalDays + days[count - 1];
    }
    totalDays = totalDays + myDate.getDate() + new Date(year + '-01-01').getDay();
  }
  var week = Math.ceil(totalDays / 7);
  return week;
}